package com.onpositive.realmview.model;

import java.util.HashSet;

import com.onpositive.semantic.model.realm.IRealmChangeListener;
import com.onpositive.semantic.model.realm.ISetDelta;

public final class GlobalChangeBroadcaster {

	
	static HashSet<IGlobalListener>listeners=new HashSet<IGlobalListener>();
	
	static HashSet<IRealmChangeListener<?>>allListeners=new HashSet<IRealmChangeListener<?>>();
	
	
	public static synchronized void addListener(IRealmChangeListener<?>allListener){
		allListeners.add(allListener);
	}
	public static synchronized void removeListener(IRealmChangeListener<?>allListener){
		allListeners.remove(allListener);
	}
	
	@SuppressWarnings("unchecked")
	public static void fireGlobal(ISetDelta<Object>d){
		for (IRealmChangeListener<Object> l:getListeners()){
			try{
			l.realmChanged(null, d);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@SuppressWarnings("rawtypes")
	public synchronized static IRealmChangeListener[]getListeners(){
		IRealmChangeListener[] array = allListeners.toArray(new IRealmChangeListener[allListeners.size()]);
		return array;
	}
	
	public static void addListener(IGlobalListener listener){
		listeners.add(listener);
	}
	public static void removeListener(IGlobalListener listener){
		listeners.remove(listener);
	}
	
	public static void notifyChanged(Object object){
		for (IGlobalListener l:listeners){
			l.changed(object);
		}
	}
}
